﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using App1AppHandler;

namespace Reborn_Server.App1AppHandler
{
    public class Spell_SummonIceGolem : TargetSpell
    {
        public void Cast(CreatureBase myCreatureBase, int CastLevel, long CreatureGUID)
        {
            List<MapItem> MapItemsWithin12;
            if (base.InitializeCast(myCreatureBase, CastLevel, CreatureGUID, false, out MapItemsWithin12) == false)
                return;

            if (myCreatureBase is Character)
            {
                Character character = (Character)myCreatureBase;
                if (character.petManager.NumPets() > 3)
                {
                    Message message = new Message();
                    message.Type = "server";
                    message.CreatureID = character.CreatureID;
                    message.Text = "You have to many pets";

                    character.PlaySound("Ding", character.MapX, character.MapY);
                    character.AddMessage(message);

                    return;
                }
                List<Pet_Controller> pets = character.petManager.GetPets();
                foreach (Pet_Controller pet in pets)
                {
                    if (pet.MyCreature.ImageURL == "IceGolem")
                    {
                        Message message = new Message();
                        message.Type = "server";
                        message.CreatureID = character.CreatureID;
                        message.Text = "You can not have more than 1 Ice Golem";

                        character.PlaySound("Ding", character.MapX, character.MapY);
                        character.AddMessage(message);

                        return;
                    }
                }
            }

            MapItem currentMapItem = null;
            foreach (MapItem mapItem in MapItemsWithin12)
            {
                if (mapItem.MapX == myCreatureBase.MapX && mapItem.MapY == myCreatureBase.MapY)
                {
                    currentMapItem = mapItem;
                    break;
                }
            }

            Creature iceGolem = new Creature(myCreatureBase.GameHandler);
            CreatureBase ig = (CreatureBase)iceGolem;
            iceGolem.petController = new Pet_Controller(ig, myCreatureBase, currentMapItem);
            iceGolem.petController.autoTauntAmount = SkillManager.AdjustForCombat(CastLevel, CastingCreatureInt / 2, CastingCreatureInt / 2);

            iceGolem.IceEater = true;
            //iceGolem.MaxHPBase = System.Convert.ToInt32((myCreatureBase.MaxHP * 1.25)) + (myCreatureBase.WisdomTotal * 2);
            iceGolem.MaxHPBase = SkillManager.AdjustForCombat(CastLevel, 0, CastingCreatureWis / 2) * 12;
            iceGolem.HP = iceGolem.MaxHPBase;

            iceGolem.MaxStaminaBase = 1000;
            iceGolem.Stamina = 1000;

            iceGolem.MaxManaBase = 0;
            iceGolem.Mana = 0;

            iceGolem.Agility = 2 * CastLevel;
            iceGolem.Armor = SkillManager.AdjustForCombat(CastLevel, 0, CastingCreatureWis / 2);
            iceGolem.ResistHoly = SkillManager.AdjustForCombat(CastLevel / 2, 100, CastingCreatureWis / 2);
            iceGolem.ResistPhysical = SkillManager.AdjustForCombat(CastLevel / 2, 100, CastingCreatureWis / 2);
            iceGolem.ResistMagic = SkillManager.AdjustForCombat(CastLevel / 2, 100, CastingCreatureWis);

            iceGolem.Dexterity = 20;
            iceGolem.Strength = 20;
            iceGolem.Wisdom = 10;
            iceGolem.Intelligence = 10;
            iceGolem.ResistIce = SkillManager.AdjustForCombat(CastLevel, 100, CastingCreatureWis); ;

            iceGolem.Defense = CastingCreatureWis;
            iceGolem.DefenseSkill = CastLevel;
            iceGolem.HandBlockWeapon = "icy fist";

            iceGolem.RushAttack = true;
            iceGolem.Water = true;
            iceGolem.Air = false;
            iceGolem.Land = true;

            CreatureAttackWeapon iceGolemWeapon = new CreatureAttackWeapon();
            iceGolemWeapon.Accuracy = 70;
            iceGolemWeapon.Description = "strikes {name}";
            iceGolemWeapon.DmgHigh = 70;
            iceGolemWeapon.DmgLow = 50;
            iceGolemWeapon.Frequency = 1;
            iceGolemWeapon.Sequence = 1;
            iceGolemWeapon.SkillLevel = CastLevel / 2;
            iceGolemWeapon.Speed = 3.1M;

            iceGolem.attackSequenceManager.Attacks.Add(iceGolemWeapon);

            iceGolem.Name = myCreatureBase.Name + "'s Ice Golem";
            iceGolem.SoundDeath = "SpiderDeath";

            //No corpse image will cause DropLoot() to not generate a corpse
            iceGolem.CorpseImageURL = "";

            iceGolem.CreatureID = myCreatureBase.GameHandler.ItemIDGen.GetCreatureUID();
            iceGolem.CreatureType = "Creature";
            iceGolem.Faction = myCreatureBase.Faction;
            iceGolem.ID = -1;
            iceGolem.ImageURL = "IceGolem";
            iceGolem.Instance = myCreatureBase.Instance;
            iceGolem.MapID = myCreatureBase.MapID;

            iceGolem.MaxMoves = 4;

            iceGolem.Timer_Regen.Start();
            iceGolem.creatureAI.Timer_CreatureRound.Start();

            MapItem mapitemToUpdate = myCreatureBase.currentMapItem;
            List<Character> CharsToUpdate = new List<Character>();
            foreach (MapItem mapitem in myCreatureBase.currentMapItem.mapItemsWithin8)
            {
                foreach (CreatureBase creaturebase in mapitem.CreaturesBases)
                {
                    if (creaturebase is Character)
                        CharsToUpdate.Add((Character)creaturebase);
                }
                if (mapitem.MapX == myCreatureBase.MapX && mapitem.MapY == myCreatureBase.MapY)
                {
                    iceGolem.SetPosition(mapitem);
                    mapitem.CreaturesBases.Add(iceGolem);
                    mapitemToUpdate = mapitem;
                }
            }
            foreach (Character character in CharsToUpdate)
            {
                //character.PlaySound(soundToPlay.Name, soundToPlay.MapX, soundToPlay.MapY);
                character.AddMapItem(mapitemToUpdate);

                if (character == myCreatureBase)
                    character.petManager.AddPet(iceGolem.petController);
            }
        }
    }
}
